package com.shouer.doc.ui.healthreportedit.presenter;

import com.android.volley.ParseError;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.shouer.doc.request.GsonRequest;
import com.shouer.doc.request.RequestUtil;
import com.shouer.doc.ui.healthreportedit.response.QueryHealthRecordDetailResponse;
import com.shouer.doc.ui.healthreportedit.response.SaveHealthRecordResponse;
import com.shouer.doc.ui.healthreportedit.response.SubmitHealthRecordResponse;
import com.shouer.doc.ui.healthreportedit.view.HealthReportEditView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by YYY-Work on 2017/8/7.
 */

public class HealthReportEditPresenterImpl implements IHealthReportEditPresenter {

    private HealthReportEditView mView;

    public HealthReportEditPresenterImpl(HealthReportEditView mView)
    {
        this.mView=mView;
        EventBus.getDefault().register(this);
    }


    @Override
    public void onDestroy() {
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void queryHealthRecordDetail(String recId) {

        Map<String, String> param = new HashMap<>();
        param.put("docId", mView.getUser().getUserId());
        param.put("recId",recId);


        final GsonRequest<QueryHealthRecordDetailResponse> request = new GsonRequest("queryHealthRecord", QueryHealthRecordDetailResponse.class, new Response.Listener<QueryHealthRecordDetailResponse>() {
            @Override
            public void onResponse(QueryHealthRecordDetailResponse response) {

                EventBus.getDefault().post(response);

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                if (error instanceof ParseError) {
                    mView.queryHealthRecordFailed("返回数据格式错误 查询新订单失败");
                } else {
                    mView.queryHealthRecordFailed("网络异常 查询新订单失败");
                }

            }
        });

        RequestUtil.sendRequest(mView.getContext(), param, request);

    }

    @Override
    public void saveHealthRecord(String recId, String subTitle, String subDesc, String defaultTags,final int type) {

        Map<String, String> param = new HashMap<>();
        param.put("docId", mView.getUserDocInfo().getDocId());
        param.put("docName", mView.getUser().getUserName());
        param.put("recId",recId);
        param.put("subTitle",subTitle);
        param.put("subDesc",subDesc);
        param.put("defaultFlag",defaultTags);


        final GsonRequest<SaveHealthRecordResponse> request = new GsonRequest("updateHealthRecord", SaveHealthRecordResponse.class, new Response.Listener<SaveHealthRecordResponse>() {
            @Override
            public void onResponse(SaveHealthRecordResponse response) {

                response.setType(type);
                EventBus.getDefault().post(response);

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                if (error instanceof ParseError) {
                    mView.saveHealthRecordFailed("返回数据格式错误 更新健康报告失败");
                } else {
                    mView.saveHealthRecordFailed("网络异常 更新健康报告失败");
                }

            }
        });

        RequestUtil.sendRequest(mView.getContext(), param, request);

    }

    @Override
    public void submitHealthRecord(String recId) {

        Map<String, String> param = new HashMap<>();
        param.put("docId", mView.getUserDocInfo().getDocId());
        param.put("recId",recId);
        param.put("type","4");

        final GsonRequest<SubmitHealthRecordResponse> request = new GsonRequest("receiveHealthRecord", SubmitHealthRecordResponse.class, new Response.Listener<SubmitHealthRecordResponse>() {
            @Override
            public void onResponse(SubmitHealthRecordResponse response) {

                EventBus.getDefault().post(response);

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                if (error instanceof ParseError) {
                    mView.submitHealthRecordFailed("返回数据格式错误 提交健康报告失败");
                } else {
                    mView.submitHealthRecordFailed("网络异常 提交健康报告失败");
                }

            }
        });

        RequestUtil.sendRequest(mView.getContext(), param, request);


    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(QueryHealthRecordDetailResponse response) {

        if (response.getErrorCode()>=0)
        {
            if (null!=response.getRecordList()&&response.getRecordList().size()>0)
            {
                mView.queryHealthRecordSuccess(response.getRecordList().get(0));
            }else
            {
                mView.queryHealthRecordFailed("没有查询到相关健康档案信息");
            }
        }else
        {
            mView.queryHealthRecordFailed(response.getErrorMsg());
        }

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(SaveHealthRecordResponse response) {

        if (response.getErrorCode()>=0)
        {
            mView.saveHealthRecordSuccess(response.getType());
        }else
        {
            mView.saveHealthRecordFailed(response.getErrorMsg());
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(SubmitHealthRecordResponse response) {

        if (response.getErrorCode()>=0)
        {
            mView.submitHealthRecordSuccess();
        }else
        {
            mView.submitHealthRecordFailed(response.getErrorMsg());
        }

    }
}
